using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using WebApplication1.Model;

namespace WebApplication1.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        private readonly ISqlSugarClient db;

        public ValuesController(SqlSugar.ISqlSugarClient db)
        {
            this.db = db;
        }

        [HttpGet]
        public IActionResult GetListAll()
        {
            var list = db.Queryable<usertable>().Where(x=>x.isdelete==0).ToList();
            return Ok(list);
        }
        [HttpPost]
        public IActionResult Addone(usertable s)
        {
            var aaa = "aaabcdef";
            var aaaa = "aaabcdefg";
            //插入返回自增列 (实体除ORACLE外实体要配置自增，Oracle需要配置序列)
           int r= db.Insertable(s).ExecuteReturnIdentity();
            return Ok(r);
        }

        [HttpGet]
        public IActionResult Delone(int id)
        {

            
            int r = db.Deleteable<usertable>().In(id).IsLogic().ExecuteCommand();
            return Ok(r);
        }

        [HttpPost]
        public IActionResult Uptone(usertable s)
        {
           //var r = new usertable();
           // r.id = 4;
           // r.name = "test";
           // r.isdelete = 0;

            //功能写法可以将插入和更新拆开，然后调用插入和更新独有特性
            var x = db.Storageable(s).ToStorage();
           var a = x.AsInsertable.ExecuteCommand();//不存在插入
           var b =  x.AsUpdateable.ExecuteCommand();//存在更新
            if(a>0)
            {
                return Ok("数据不存在已添加");
            }
            else if(b>0)
            {
                return Ok("修改成功");
            }
            else
            {
                return Ok("失败");
            }
            
        }
    }
}
